using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MultiLayer
{
    public class SelectTool : Tool
    {
        public SelectTool(Control pContainer)
            : base(pContainer)
        {
            this.mCursor = Cursors.Hand;
            this.mIsMoving = false;
        }

        public override void StylusDown(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.StylusDownData data)
        {
            if (Screen.Current.SelectedEntity != null && Screen.Current.SelectedEntity.BoundingBox.IntersectsWith(new Rectangle(data[0], data[1], 1, 1)))
            {
                this.mIsMoving = true;
                this.mLastPoint = new Point(data[0], data[1]);
            }
            else
            {
                if (Screen.Current.HitTest(data[0], data[1]) != null)
                {
                    this.RefreshScreen(Screen.Current.SelectedEntity.BoundingBox);
                }
            }
        }

        public override void Packets(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.PacketsData data)
        {
            if (this.mIsMoving && Screen.Current.SelectedEntity != null)
            {
                Point delta = new Point(data[0] - this.mLastPoint.X, data[1] - this.mLastPoint.Y);
                Rectangle oldRect = Screen.Current.SelectedEntity.BoundingBox;
                Screen.Current.SelectedEntity.Move(delta.X, delta.Y);
                if (delta.X != 0 && delta.Y != 0)
                {
                    this.RefreshScreen(Rectangle.Union(oldRect, Screen.Current.SelectedEntity.BoundingBox));
                }
                this.mLastPoint = new Point(data[0], data[1]);
            }
        }

        public override void StylusUp(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.StylusUpData data)
        {
            if (this.mIsMoving && Screen.Current.SelectedEntity != null)
            {
                this.mIsMoving = false;
                this.RefreshScreen(Screen.Current.SelectedEntity.BoundingBox);
            }
        }

        private bool mIsMoving;
        private Point mLastPoint;
    }
}
